﻿using System.Net;
using System.Numerics;
using System.Web;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using up6.db.biz;
using up6.db.database.down2.sql;
using up6.db.database.up6.sql;
using up6.db.model;
using up6.utils;
using Up6.core.Up6Manager.Biz;
using Up6.core.Up6Manager.Models.Req;

namespace Up6.core.Up6Manager;

public class Up6NoSqlAction
{
    /// <summary>
    /// 文件初始化
    /// </summary>
    /// <param name="req"></param>
    /// <returns></returns>
    public async Task<IActionResult> CreateAsync(FileInf f)
    {
        //参数为空
        if (
            string.IsNullOrEmpty(f.md5)
            || string.IsNullOrEmpty(f.uid)
            || string.IsNullOrEmpty(f.sizeLoc)
        )
        {
            return new ContentResult { 
                Content= f.callback + "({\"value\":null,\"ret\":false,\"error\":\"参数为空\"})",
                ContentType= "application/json; charset=utf-8",
                StatusCode =200
            };
        }

        if (!ConfigReader.inFileExts(f.pathLoc))
        {
            string m = f.callback + "({\"value\":\"0\",\"ret\":false,\"error\":\"文件类型非法\"})";//返回jsonp格式数据。

            return new ContentResult
            {
                Content = m,
                ContentType = "application/json; charset=utf-8",
                StatusCode = 200
            };
        }

        WebSafe ws = new WebSafe();
        var ret = ws.validToken(f.token, f);
        //token验证失败
        if (!ret)
        {
            string m = f.callback + "({\"value\":\"0\",\"ret\":false,\"error\":\"token error\"})";//返回jsonp格式数据。

            return new ContentResult
            {
                Content = m,
                ContentType = "application/json; charset=utf-8",
                StatusCode = 200
            };
        }

        PathBuilderUuid pb = new PathBuilderUuid();
        f.pathSvr = pb.genFile(f.uid,f );
                
        try
        {
            //创建文件
            var fw = ConfigReader.blockWriter();
            f.object_id = await fw.makeAsync(f);
            f.object_key = f.getObjectKey();
        }
        catch (IOException ie)
        {
            return new ContentResult
            {
                Content = f.callback+string.Format("(\"value\":\"\",\"error\":\"{0}\",\"ret\":false)",ie.Message),
                ContentType = "application/json; charset=utf-8",
                StatusCode = 200
            };
        }

        //将路径转换成相对路径
        f.pathSvr = pb.absToRel(f.pathSvr);

        string jv = JsonConvert.SerializeObject(f);
        jv = HttpUtility.UrlEncode(jv);
        jv = jv.Replace("+", "%20");
        string json = f.callback + "({\"value\":\"" + jv + "\",\"ret\":true})";//返回jsonp格式数据。

        return new ContentResult
        {
            Content = json,
            ContentType = "application/json; charset=utf-8",
            StatusCode = 200
        };
    }

    public IActionResult complete(FileInf f)
    {
        int ret = 0;
        if (!string.IsNullOrEmpty(f.id))
        {            
            ret = 1;
        }
        return new ContentResult
        {
            Content = f.callback+"("+ret+")",
            ContentType = "application/json; charset=utf-8",
            StatusCode = 200
        };
    }
    public IActionResult del(FileInf f)
    {
        int ret = 0;
        if (!string.IsNullOrEmpty(f.id))
        {
            ret = 1;
        }
        return new ContentResult
        {
            Content = f.callback + "(" + ret + ")",
            ContentType = "application/json; charset=utf-8",
            StatusCode = 200
        };
    }
    public IActionResult list(FileInf f)
    {      
        return new ContentResult
        {
            Content = f.callback + "({\"value\":null})",
            ContentType = "application/json; charset=utf-8",
            StatusCode = 200
        };
    }

    public IActionResult process(FileInf f)
    {
        int ret = 0;
        if (!string.IsNullOrEmpty(f.id)&&
            !string.IsNullOrEmpty(f.perSvr))
        {            
            ret = 1;
        }
        return new ContentResult
        {
            Content = f.callback + "(" + new { state = ret } + ")",
            ContentType = "application/json; charset=utf-8",
            StatusCode = 200
        };
    }

    /// <summary>
    /// 文件夹初始化
    /// </summary>
    /// <param name="f"></param>
    /// <returns></returns>
    public IActionResult fd_create(FileInf f)
    {
        //生成路径
        PathBuilderUuid pb = new PathBuilderUuid();
        f.pathSvr = pb.genFolder(f);
        //创建文件夹错误
        if(!PathTool.mkdir(f.pathSvr))
        {
            var js = new JObject { { "msg", "create dir error" } };

            return new ContentResult
            {
                Content = f.callback + string.Format("({0})",JsonConvert.SerializeObject(js)),
                ContentType = "application/json; charset=utf-8",
                StatusCode = 500
            };
        }

        //创建层级信息文件
        FolderSchema fs = new FolderSchema();
        if (!fs.create(f))
        {
            var js = new JObject { { "msg", "create schema file error" } };
            return new ContentResult
            {
                Content = f.callback + string.Format("({0})",JsonConvert.SerializeObject(js)),
                ContentType = "application/json; charset=utf-8",
                StatusCode = 500
            };
        }        
        var json = JsonConvert.SerializeObject(f);
        json = WebUtility.UrlEncode(json);
        json = json.Replace("+", "%20");
        var jo = new JObject { { "value",json} };

        return new ContentResult
        {
            Content = f.callback + string.Format("({0})",JsonConvert.SerializeObject(jo)),
            ContentType = "application/json; charset=utf-8",
        };
    }

    public IActionResult fd_complete(FileInf f)
    {
        int ret = 0;
        if(!string.IsNullOrEmpty(f.id))
        {            
            ret = 1;
        }

        return new ContentResult
        {
            Content = f.callback + "(" + ret + ")",
            ContentType = "application/json; charset=utf-8",
        };
    }

    public IActionResult fd_del(FileInf f)
    {
        int ret = 0;
        if (!string.IsNullOrEmpty(f.id))
        {            
            ret = 1;
        }

        return new ContentResult
        {
            Content = f.callback + "(" + new { value = ret } + ")",
            ContentType = "application/json; charset=utf-8",
        };
    }
}
